package seeders

import (
	"github.com/goravel/framework/facades"
	"goravel/app/models"
)

type KindSeeder struct {
}

// Signature The name and signature of the seeder.
func (s *KindSeeder) Signature() string {
	return "KindSeeder"
}

// Run executes the seeder logic.
func (s *KindSeeder) Run() error {
	pkinds := []models.Kind{
		{
			Name: "侧翻车",
			Children: []models.Kind{
				{Name: "联合"},
			},
		},
		{
			Name: "框架车",
			Children: []models.Kind{
				{Name: "进口上置式"},
				{Name: "国产下置式"},
				{Name: "国产上置式"},
			},
		},
		{
			Name: "废钢车",
			Children: []models.Kind{
				{Name: "联合"},
			},
		},
		{

			Name: "废钢容器车",
			Children: []models.Kind{
				{Name: "联合"},
			},
		},
		{
			Name: "吊斗车",
			Children: []models.Kind{
				{
					Name: "20吨吊斗车",
					Children: []models.Kind{
						{Name: "联合"},
					},
				},
				{
					Name: "11吨吊斗车",
					Children: []models.Kind{
						{Name: "东风天龙"},
					},
				},
			},
		},
		{
			Name: "真空污泥车",
			Children: []models.Kind{
				{Name: "东风天龙"},
			},
		},
		{
			Name: "吸引罐车",
			Children: []models.Kind{
				{Name: "东风天龙"},
			},
		},
		{
			Name: "危险品车",
			Children: []models.Kind{
				{Name: "东风天龙"},
			},
		},
		{
			Name: "外租车",
			Children: []models.Kind{
				{Name: "东风天龙"},
			},
		},
		{Name: "铲车"},
	}
	//递归创建

	return RecursionCrt(pkinds)
}

// 递归调用，解析菜单数据Recursion
func RecursionCrt(kinds []models.Kind) error {
	for _, kind := range kinds {
		err := facades.Orm().Query().Model(&models.Kind{}).Create(&kind)
		if err != nil {
			return err
		}
		if kind.Children != nil {
			for i := 0; i < len(kind.Children); i++ {
				kind.Children[i].Pid = kind.ID
			}
			err := RecursionCrt(kind.Children)
			if err != nil {
				return err
			}
		}
	}
	return nil
}
